<template>
  <div class="login">
    <div class="header"></div>
    <div class="main"></div>
    <div class="login-page-layer login-page-layer-area">
      <ul class="circles">
        <li v-for="n in 10" :key="n"></li>
      </ul>
    </div>
    <div class="login-page-layer login-page-layer-time">{{time}}</div>
    <div class="login-form">
      <span>Admin</span>
      <el-form :model="loginForm" :rules="rules" ref="loginForm" class="loginForm">
        <el-form-item prop="username" class="login-item">
          <span class="loginTips"></span>
          <el-input @keyup.enter.native="submitForm('loginForm')" class="area" type="text" placeholder="用户名" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password" class="login-item">
          <span class="loginTips"></span>
          <el-input @keyup.enter.native="submitForm('loginForm')" class="area" type="password" placeholder="密码" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">登 陆</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="footer">
      <p>Copyright <i class="fa fa-copyright"></i>2021 Projects <a href="#"></a></p>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/login";
import dayjs from "dayjs"
//import { messages } from "@/assets/js/common.js";

export default {
  name: "login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          { min: 5, max: 15, message: "长度 5 - 15 个字符" },
        ],
        password: [
          { required: true, message: "密码不能为空" },
          { min: 6, max: 15, message: "长度 6 - 15 个字符" },
        ],
      },
      timeInterval: null,
      time: dayjs().format('HH:mm:ss'),
    };
  },
  mounted() {
    this.timeInterval = setInterval(() => {
      this.refreshTime()
    }, 1000)
  },
  beforeDestroy () {
    clearInterval(this.timeInterval)
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let roles = [];
          login(this.loginForm)
            .then((res) => {
              this.$store.commit("COMMIT_TOKEN", res.data);
              this.$message("sucess", res.message);
              this.$router.push({path: "/home"});
            })
            .catch((err) => {
              this.$message("error", res.message);
            });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    refreshTime () {
      this.time = dayjs().format('HH:mm:ss')
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background-color: #f0f2f5;
  position: relative;

  .header {
    width: 100%;
    height: 60px;
  }

  .login-page-layer {
    .circles {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      overflow: hidden;
      margin: 0;
      padding: 0;

      li {
        position: absolute;
        display: block;
        list-style: none;
        width: 20px;
        height: 20px;
        background: #FFF;
        animation: animate 25s linear infinite;
        bottom: -200px;
        @keyframes animate {
          0%{
            transform: translateY(0) rotate(0deg);
            opacity: 1;
            border-radius: 0;
          }
          100%{
            transform: translateY(-1000px) rotate(720deg);
            opacity: 0;
            border-radius: 50%;
          }
        }
        &:nth-child(1) {
          left: 15%;
          width: 80px;
          height: 80px;
          animation-delay: 0s;
        }
        &:nth-child(2) {
          left: 5%;
          width: 20px;
          height: 20px;
          animation-delay: 2s;
          animation-duration: 12s;
        }
        &:nth-child(3) {
          left: 70%;
          width: 20px;
          height: 20px;
          animation-delay: 4s;
        }
        &:nth-child(4) {
          left: 40%;
          width: 60px;
          height: 60px;
          animation-delay: 0s;
          animation-duration: 18s;
        }
        &:nth-child(5) {
          left: 65%;
          width: 20px;
          height: 20px;
          animation-delay: 0s;
        }
        &:nth-child(6) {
          left: 75%;
          width: 150px;
          height: 150px;
          animation-delay: 3s;
        }
        &:nth-child(7) {
          left: 35%;
          width: 200px;
          height: 200px;
          animation-delay: 7s;
        }
        &:nth-child(8) {
          left: 50%;
          width: 25px;
          height: 25px;
          animation-delay: 15s;
          animation-duration: 45s;
        }
        &:nth-child(9) {
          left: 20%;
          width: 15px;
          height: 15px;
          animation-delay: 2s;
          animation-duration: 35s;
        }
        &:nth-child(10) {
          left: 85%;
          width: 150px;
          height: 150px;
          animation-delay: 0s;
          animation-duration: 11s;
        }
      }
    }

    ul {
      display: block;
      list-style-type: disc;
      margin-block-start: 1em;
      margin-block-end: 1em;
      margin-inline-start: 0px;
      margin-inline-end: 0px;
      padding-inline-start: 40px;
    }
  }

  .login-page-layer-time {
    position: relative;
    font-size: 24em;
    font-weight: 700;
    color: rgba(0,0,0,.03);
    overflow: hidden;
    text-align: center;
    padding: 160px 0 10px 0;
  }

  .login-form {
    position: absolute;
    width: 280px;
    top: 130px;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
    z-index: 999;

    span {
      font-size: 2.2em;
      display: block;
      text-align: center;
    }

    .loginForm {
      background: #fff;
      padding: 20px 20px 1px 20px;
      border-radius: 5px;
      margin-top: 30px;
    }
  }

  .footer {
    height: 10px;
    text-align: center;
    margin-bottom: 10px;
    font-size: 12px;
    line-height: 12px;
    color: #606266;
  }
}
.el-button {
  width: 100%;
}
</style>